package com.ysd.crm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.crm.entity.RoleModules;
import com.ysd.crm.entity.Roles;
import com.ysd.crm.service.RolesService;
import com.ysd.crm.utils.LayResult;
import com.ysd.crm.utils.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RequestMapping("/roles")
@Api
@RestController
public class RolesController {

	@Autowired
	private RolesService rService;

	/**
	 * 多条件分页查询所有
	 * 
	 * @param r
	 * @return
	 */
	@ApiOperation("多条件分页查询所有-ydd")
	@GetMapping("getRoles")
	public String getRoles(Roles r) {
		r.setPage((r.getPage() - 1) * r.getLimit());
		List<Roles> list = rService.findRoles(r);
		if (list != null) {
			int i = rService.findCount(r);
			return LayResult.toClient("0", "查询成功", i, list);
		}
		return LayResult.toClient("1", "查询失败");
	}

	/**
	 * 添加
	 * 
	 * @param r
	 * @return
	 */
	@ApiOperation("添加-ydd")
	@PostMapping("insertRoles")
	public Object insertRoles(Roles r) {

		Roles user = rService.findName(r);
		if (user == null) {
			int i = rService.addRoles(r);
			if (i > 0) {
				return LayResult.toClient("0", "添加成功");
			}
			return LayResult.toClient("0", "添加失败");
		} else {
			return LayResult.toClient("1", "角色已经存在");
		}

	}

	/**
	 * 修改
	 * 
	 * @param r
	 * @return
	 */
	@ApiOperation("修改-ydd")
	@PostMapping("updateRoles")
	public String updateRoles(Roles r) {

		Roles roles = rService.findName(r);
		if (roles == null) {
			int i = rService.upRoles(r);
			if (i > 0) {
				return LayResult.toClient("0", "修改成功");
			}
			return LayResult.toClient("0", "修改失败");
		} else {
			return LayResult.toClient("1", "角色名已经存在");
		}
	}

	/**
	 * 删除
	 * 
	 * @param r
	 * @return
	 */
	@ApiOperation("删除-ydd")
	@PostMapping("delRoles")
	public Object delete(String Id , RoleModules rm) {
		List<RoleModules> list = rService.getId(rm);
		if (list.size() == 0) {
			int n = rService.delRoles(Id);
			
			if (n>0) {
				return LayResult.toClient("0", "删除成功"); 
			}
			return LayResult.toClient("1", "删除失败"); 
		}else {
			return LayResult.toClient("1", "该角色拥有模块，删除失败"); 
		}
	}
	@ApiOperation("-ydd")
	@PostMapping("getId")
	public Object getID(RoleModules rm) {
		List<RoleModules> list = rService.getId(rm);
		if (list!=null) {
			return LayResult.toClient("0", "查询成功", null, list);
		}
		return LayResult.toClient("0", "查询成功", null, list);
		
	}

	/**
	 * ytt
	 * 查询所有角色
	 * @return
	 */
	@ApiOperation("查询所有角色-ytt")
	@PostMapping("getAllRoles")
	public Object getAllRoles() {
		List<Roles> list = rService.findAllRoles();
		System.out.println(list);
		if (list != null) {
			return LayResult.toClient("0", "查询成功", null, list);
		}
		return LayResult.toClient("1", "查询失败");
	}
	/**
	 * ytt
	 * 根据用户id查询角色id
	 * @return
	 */
	@ApiOperation("根据用户id查询角色id-ytt")
	@PostMapping("getUserIdByRolesId")
	public Object getUserIdByRolesId(String Id) {
		List<String> list = rService.findUserIdByRolesId(Id);
		if (list != null) {
			return LayResult.toClient("0", "查询成功", null, list);
		}
		return LayResult.toClient("1", "查询失败");
	}
	/**
	 * ytt
	 * 给用户添加角色
	 * @return
	 */
	@ApiOperation("给用户添加角色-ytt")
	@PostMapping("addUserRoles")
	public Object addUserRoles(String UserId,String RoleIds) {
		String[] RoleId=RoleIds.split(",");
		for (String string : RoleId) {
			System.out.println(string);
		}
		int n = rService.insertUserRoles(UserId, RoleId);
		if (n >0) {
			return LayResult.toClient("0", "添加角色成功");
		}
		return LayResult.toClient("1", "添加角色失败");
	}
	/**
	 * ytt
	 * 给用户删除角色
	 * @return
	 */
	@ApiOperation("给用户删除角色-ytt")
	@PostMapping("delUserRoles")
	public Object delUserRoles(String UserId,String RoleIds) {
		String[] RoleId=RoleIds.split(",");
		int n = rService.deleteUserRoles(UserId, RoleId);
		if (n >0) {
			return LayResult.toClient("0", "删除角色成功");
		}
		return LayResult.toClient("1", "删除角色失败");
	}
}
